#ifndef MAIN_H
#define MAIN_H


#include <QCoreApplication>
#include <QDesktopServices>
#include <QDebug>
#include <QList>
#include <QStringList>
#include <QObject>
#include <QDateTime>
#include <QDate>
#include <QDir>
#include <QFile>
#include <QDomDocument>
#include <QResource>
#include <QTextCodec>



#define _PROGRAM_NAME_ "JuffEd Txt Editor"
#define _PROGRAM_NAME_DOMAINE_ "juff.mezomish.org"
#define _ORGANIZATION_NAME_ "JuffEd_Producer"
#define _PROGRAM_VERSION_ "version 0.2"
#define _PROGRAM_TITLE_  _PROGRAM_NAME_" - "_PROGRAM_VERSION_
#define _PROGRAM_SHORT_NAME "jufftxt"







/* remove a file */
static inline bool qt_unlink(QString fullFileName)
{
    if (fullFileName.size() > 0) {
    QFile f( fullFileName );
	if ( f.exists(fullFileName) ) {
       if (f.remove()) {
        return true;
       }
	}
    }
return false;
}

static inline bool is_file(QString fullFileName)
{
    if (!fullFileName.size() > 3) {
      return false;  
    }
    QFile f( fullFileName );
	if ( f.exists(fullFileName) ) {
    return true;  
	} else {
	return false;
    }
}



static inline bool Copy_To(QString inputfile, const QString outfile)
{
    QString ultimacartellaaperta = outfile.left(outfile.lastIndexOf("/"))+"/";
    QDir dira(ultimacartellaaperta);
    dira.mkpath(ultimacartellaaperta);
    
    if (inputfile.startsWith(":/")) {
        QResource actual(inputfile);
        /////qDebug() << "rvalid->" << actual.isValid() << "|" << actual.absoluteFilePath();
        QFile fr(actual.absoluteFilePath());
        return fr.copy(outfile);
    }
    
    QFile Imaged(inputfile); 
    return Imaged.copy(outfile);
}


/* write a file to utf-8 format */
static inline bool fwriteutf8(QString fullFileName,QString xml)
{
    if (fullFileName.contains("/", Qt::CaseInsensitive)) {
    QString ultimacartellaaperta = fullFileName.left(fullFileName.lastIndexOf("/"))+"/";
    QDir dira(ultimacartellaaperta);
    if ( dira.mkpath(ultimacartellaaperta) ) { } else {
    return false;
    }
    }
    
        QTextCodec *codecx;
        codecx = QTextCodec::codecForMib(106);
        QFile f( fullFileName );
        if ( f.open( QFile::WriteOnly | QFile::Text ) )
        {
        QTextStream sw( &f );
        sw.setCodec(codecx);
        sw << xml;
        f.close();
        return true;
        }
        return false;
}


/* read the contenet of a local file as qstring */
static inline QString fopenutf8(QString fullFileName)
{
    QString inside = "";
    QFile file(fullFileName); 
    if (file.exists()) {
                if (file.open(QFile::ReadOnly | QFile::Text)) {
                    inside = QString::fromUtf8(file.readAll());
                  file.close();
                }
    }

return inside;
}


#if defined Q_WS_MAC
#define WEBAGENTNAME "Mac QT4 / PPK_W @ciz.ch"
#endif
#if defined Q_WS_WIN
#define WEBAGENTNAME "Windows QT4 / PPK_W @ciz.ch"
#endif
#if defined Q_WS_X11
#define WEBAGENTNAME "Unix QT4 / PPK_W @ciz.ch"
#endif



#define APPCACHEDIR \
               QString( "%1/.%2/" ).arg(QDir::homePath(),_PROGRAM_SHORT_NAME) 
               
#define APPLICATION_SETTING \
               QString( "%1juffed.conf" ).arg( APPCACHEDIR ) 
#define APPLICATION_FILE \
               QString( "%1rec.xml" ).arg( APPCACHEDIR ) 
               
#define WORDLIST_FILE \
               QString( "%1wordlist" ).arg( APPCACHEDIR ) 
               

           
    
#endif

